/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.report;

import java.util.Vector;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.border.EmptyBorder;

/**

 * A <code>ReportWriter</code>, which is formatting reports by using HTML. The result in the
 * desired format is stored in the variable <code>reportText</code>
 * and can be readed out by calling the processing operation
 * <code>getReportText()</code>.
 *
 * @author Hakan Keles
 */
public class HTMLReportWriter extends ReportWriter {

    /**
     * This variable is allocated with the result of the conversion. Format: HTML.
     */
    private String reportText = "";

    /**
     * Reset the variable <code>reportText</code>.
     */
    public void resetReportText() {
        this.reportText = "";
    }

    /**
     * Conversion of a <code>Paragraph</code>-object to HTML.
     * @param p the converted paragraph
     * @return HTML-text
     */
    public String paragraph2HTML(Paragraph p) {
        if (p instanceof CompositeParagraph) {
            return this.paragraph2HTML((CompositeParagraph) p);
        } else {
            return this.paragraph2HTML((SimpleParagraph) p);
        }
    }

    /**
     * Conversion of a <code>CompositeParagraph</code>-object to HTML.
     * @param p the converted composite paragraph
     * @return HTML-text
     */
    public String paragraph2HTML(CompositeParagraph p) {
        StringBuffer result = new StringBuffer();

        for (int i = 0; i < p.getNumParagraphs(); i++) {
            result.append("<p>" + p.getParagraphAt(i) + "</p>");
        }

        return result.toString();
    }

    /**
     * Conversion of a <code>SimpleParagraph</code>-object to HTML.
     * @param p the converted simple paragraph
     * @return HTML-text
     */
    public String paragraph2HTML(SimpleParagraph p) {
        return "<p>" + p.toString() + "</p>";
    }

    /**
     * Produce a HTML-header-text.
     * @return HTML-text
     */
    public String getHeader() {
        StringBuffer result = new StringBuffer();

        result.append("<html><head><title></title></head><body>");
        return result.toString();
    }

    /**
     * Produce a HTML-trailer-text.
     * @return HTML-text
     */
    public String getTrailer() {
        return "</body></html>";
    }

    /**
     * Process the passed report und deposit it in the desired format. Later a read-out of the report
     * is possible with method <code>getReportText()</code>.
     *
     * @param f the passed report
     */
    public void process(AllFlightsOfCustomerReport f) {
        this.resetReportText();
        StringBuffer result = new StringBuffer();
        result.append("<H1>" + f.getTitle() + "</H1>");
        result.append("<table><tr>");
        result.append("<td valign=\"top\"><b>" + paragraph2HTML(f.getHeader()) + "</b></td>");
        result.append("<td valign=\"top\">" + paragraph2HTML(f.getImprint()) + "</td>");
        result.append("</tr><tr><td></td><td>" +
                f.getDateTime().toString() + "</td></tr></table>");

        Vector<Row> rows = f.getRows();
        result.append("<table>");

        for (Row row : rows) {
            result.append("<tr>");
            for (int k = 0; k < row.getNumColumns(); k++) {
                result.append("<td valign=\"top\">" + row.getColumnAt(k) + "</td>");
            }
            result.append("</tr>");
        }

        result.append("</table>");


        this.reportText = result.toString();

    }

    /**
     * Process the passed report und deposit it in the desired format. Later a read-out of the report
     * is possible with method <code>getReportText()</code>.
     *
     * @param fca the passed report
     */
    public void process(AllFlightsOfCustomerWithAirlineReport fca) {
        this.resetReportText();
        StringBuffer result = new StringBuffer();
        result.append("<H1>" + fca.getTitle() + "</H1>");
        result.append("<table><tr>");
        result.append("<td valign=\"top\"><b>" + paragraph2HTML(fca.getHeader()) + "</b></td>");
        result.append("<td valign=\"top\">" + paragraph2HTML(fca.getImprint()) + "</td>");
        result.append("</tr><tr><td></td><td>" +
                fca.getDateTime().toString() + "</td></tr></table>");

        Vector<Row> rows = fca.getRows();
        result.append("<table>");

        for (Row row : rows) {
            result.append("<tr>");
            for (int k = 0; k < row.getNumColumns(); k++) {
                result.append("<td valign=\"top\">" + row.getColumnAt(k) + "</td>");
            }
            result.append("</tr>");
        }

        result.append("</table>");


        this.reportText = result.toString();
    }

    /**
     * Process the passed report und deposit it in the desired format. Later a read-out of the report
     * is possible with method <code>getReportText()</code>.
     *
     * @param fs the passed report
     */
    public void process(FlightSeatingReport fs) {
        this.resetReportText();
        StringBuffer result = new StringBuffer();
        result.append("<H1>" + fs.getTitle() + "</H1>");
        result.append("<table><tr>");
        result.append("<td valign=\"top\"><b>" + paragraph2HTML(fs.getHeader()) + "</b></td>");
        result.append("<td valign=\"top\">" + paragraph2HTML(fs.getImprint()) + "</td>");

        result.append("</tr><tr><td></td><td>" +
                fs.getDateTime().toString() + "</td></tr></table>");

        Vector<Row> rows = fs.getRows();
        result.append("<table border=\"1\">");

        for (Row row : rows) {
            result.append("<tr>");
            for (int k = 0; k < row.getNumColumns(); k++) {
                result.append("<td valign=\"top\">" + row.getColumnAt(k) + "&nbsp;</td>");
            }
            result.append("</tr>");
        }

        result.append("</table>");

        this.reportText = result.toString();
    }

    /**
     * Read-out the result of the last called processing method..
     *
     * @return String in HTML
     */
    public String getReportText() {
        return this.getHeader() + this.reportText + this.getTrailer();
    }
}
